package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.example.bean.DataBean;
import com.example.bean.TestData;
import com.example.calladapter.CustomCall;
import com.example.calladapter.Result;
import com.example.converter.CustomConverterFactory;
import com.example.okhttp.OkhttpManager;
import com.example.okhttp.RHandler3;
import com.example.okhttp.RetrofitBase;
import com.example.retrofitinterface.Net2;
import com.example.retrofitinterface.Net2Service;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.okhttp.retrofit.BaseResponseBodyHandler;
import com.okhttp.retrofit.BaseResponseHandler;
//import com.okhttp.retrofit.GeneralRequestService;
import com.okhttp.retrofit.RetrofitClient;
import java.io.IOException;
import java.util.Date;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends Activity {

  TextView textview, textView2, textview3, textview4;
  RetrofitBase retrofitBase;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textview = findViewById(R.id.textview);
    textView2 = findViewById(R.id.textview2);
    textview3 = findViewById(R.id.textview3);
    textview4 = findViewById(R.id.textview4);

    retrofitBase = new RetrofitBase(this);

    findViewById(R.id.buttonpost).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        doSomething();
      }
    });
    findViewById(R.id.buttonget).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        doSomething2();
      }
    });

    findViewById(R.id.buttongetOKhttp).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        doSomething3();
      }
    });
    findViewById(R.id.buttonpostOKhttp).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        doSomething4();
      }
    });
  }

  private void doSomething() {
    //    NetRequeset.getIndexPost("testPost",new Callback<DataBean>() {
    //      @Override
    //      public void onResponse(Call<DataBean> call, Response<DataBean> response) {
    //        DataBean data = response.body();
    //        Date date = new Date();
    //        textview.setText(date.getMinutes() + " " + date.getSeconds() + ":\n" + data + "");
    //      }
    //
    //      @Override
    //      public void onFailure(Call<DataBean> call, Throwable t) {
    //        textview.setText("请求失败！");
    //      }
    //    });
    //RetrofitClient.getInstance().addConverterFactory(CustomConverterFactory.create());
    
    /*Net2Service.doGet1("test", new BaseResponseHandler<TestData, Result<TestData>>() {
      @Override
      public void onBeforeExecute() {
        textview.setText("request ....");
      }
  
      @Override
      public void onAfterFailure(String uniqueIdentifier, String code, String message) {
        textview.setText("请求失败！");
      }
  
      @Override
      public void onHandler(String uniqueIdentifier, String code, String message, TestData result) {
        TestData data = result;
        Date date = new Date();
        textview.setText(date.getMinutes() + " " + date.getSeconds() + ":\n" + data + "");
      }
    });*/

    //    Net2Service.doGet3("test", new Callback<DataBean>() {
    //      @Override
    //      public void onResponse(Call<DataBean> call, Response<DataBean> response) {
    //
    //      }
    //
    //      @Override
    //      public void onFailure(Call<DataBean> call, Throwable t) {
    //
    //      }
    //    });
  }

  private void doSomething2() {
    BaseResponseBodyHandler<DataBean> baseResponseHandler = new BaseResponseBodyHandler<DataBean>() {
      @Override
      public boolean onHandler(int what, String code, String message, DataBean result) {
        final DataBean data = result;
        final Date date = new Date();
        textView2.setText(date.getMinutes() + " " + date.getSeconds() + ":\n" + data + "");
        return false;
      }
    };
    //GeneralRequestService.doGet("city", baseResponseHandler);
  }

  private void doSomething3() {
    RHandler3<DataBean> responseHandler = new RHandler3<DataBean>() {
      @Override
      public boolean onHandler(int what, String code, String message, DataBean result) {
        final DataBean data = result;
        final Date date = new Date();
        textview3.setText(date.getMinutes() + " " + date.getSeconds() + ":\n" + data + "");
        return false;
      }
    };
    Net2 service = RetrofitClient.getInstance().createService("http://192.168.10.78:3001/",Net2.class);
    Call<String> stringCall = service.doGet5();
    stringCall.enqueue(new Callback<String>() {
      @Override
      public void onResponse(Call<String> call, Response<String> response) {
        System.out.println("response"+response.body());
      }

      @Override
      public void onFailure(Call<String> call, Throwable t) {

      }
    });
    //CustomCall<com.example.calladapter.Result<DataBean>> call = service.doGet2("city");
    //call.enqueue(new Callback<com.example.calladapter.Result<DataBean>>() {
    //  @Override
    //  public void onResponse(Call<com.example.calladapter.Result<DataBean>> call, Response<com.example.calladapter.Result<DataBean>> response) {
    //    com.example.calladapter.Result<DataBean> body = response.body();
    //    System.out.println("call:"+body);
    //  }
    //
    //  @Override
    //  public void onFailure(Call<com.example.calladapter.Result<DataBean>> call, Throwable t) {
    //
    //  }
    //});
    //
    //CustomCall<DataBean> call5 = service.doGet5("city");
    //call5.enqueue(new Callback<DataBean>() {
    //  @Override
    //  public void onResponse(Call<DataBean> call, Response<DataBean> response) {
    //    DataBean body = response.body();
    //    System.out.println("call5: "+body);
    //  }
    //
    //  @Override
    //  public void onFailure(Call<DataBean> call, Throwable t) {
    //
    //  }
    //});

    //Net2Service.enqueue("doGet2", responseHandler,"city");
  }

  private void doSomething4() {
    OkhttpManager.getInstance().request(new okhttp3.Callback() {
      @Override
      public void onFailure(okhttp3.Call call, IOException e) {
        e.printStackTrace();
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            textview4.setText("请求失败！");
          }
        });
      }

      @Override
      public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
        String json = "";
        if (response.isSuccessful()) {
          if (response.networkResponse() != null) {
            Log.d("MainActivity", "onResponse: Service data................");
            json = response.body().string();
            Log.d("MainActivity", "onResponse: " + json);
          } else {
            Log.d("MainActivity", "onResponse: cache data................");
            json = response.body().string();
            Log.d("MainActivity", "onResponse: " + json);
          }
        } else {
          if (response.cacheResponse() != null) {
            Log.d("MainActivity", "onResponse: cache data......service failuer..........");
            json = response.body().string();
            Log.d("MainActivity", "onResponse: " + json);
          }
        }
        Gson gson = new Gson();
        final DataBean data = (DataBean) gson.fromJson(json, new TypeToken<DataBean>() {
        }.getRawType());
        final Date date = new Date();
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            textview4.setText(date.getMinutes() + " " + date.getSeconds() + ":\n" + data + "");
          }
        });

      }
    }, "testPOST_OkHttp", "POST");
  }
}
